<template>
  <a-modal
    :title="`产品成本${model && model.id > 0 ? '编辑' : '新增'}`"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok')
      }
    "
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form" :labelCol="{ sm: { span: 4 } }" :wrapperCol="{ sm: { span: 20 } }" :selfUpdate="true">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item v-show="model && model.id > 0" label="ID">
          <a-input v-decorator="['id', { initialValue: 0 }]" disabled />
        </a-form-item>

        <a-form-item label="价格(元)">
          <a-input
            v-decorator="['price', { rules: [{ required: true, message: '价格不能为空' }] }]"
            placeholder="请输入价格"
            
          />
        </a-form-item>
        <a-form-item label="平台">
          <AWM-select
            v-decorator="['platformId', { rules: [{ required: true, message: '平台不能为空' }] }]"
            placeholder="请输入平台"
            funStr="platformByNoAuth"
          ></AWM-select>
        </a-form-item>
        <a-form-item label="产品">
          <AWM-select
            v-decorator="['productId', { rules: [{ required: true, message: '产品不能为空' }] }]"
            placeholder="请输入产品"
            funStr="productByNoAuth"
          ></AWM-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import cloneDeep from 'lodash.clonedeep'
import { platformList, useList } from '@/utils/other'
import AWMSelect from '@/components/AWMSelect'
// 表单字段
const fields = ['id', 'price', 'platformId', 'productId']

export default {
  components: {
    AWMSelect,
  },
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    return {
      form: this.$form.createForm(this),
      inviteUserList: [],
      timeout: null,
      fetching: false,
      projectType: [],
      platformList,
      useList,
    }
  },
  created() {
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', (n) => {
      if (this.model) {
        this.form.setFieldsValue(pick(this.model, fields))
      }
    })
  },
  methods: {},
  computed: {},
  watch: {},
}
</script>
